﻿#version 330 core

uniform sampler2D Texture;
uniform sampler2D ShadowTexture;
uniform int TextureEnabled;

out vec4 out_frag_color;
in vec2 pass_texcoord;
in vec3 pass_normal;
in vec3 pass_shadow_coord;

vec4 ambientColor = vec4(0.2, 0.2, 0.2, 1);
vec4 diffuseColor = vec4(1, 1, 1, 1);
vec3 lightDirection = vec3(1, 1, 0);
vec4 lightColor = vec4(1, 1, 1, 1);
float lightIntensity = 10;
vec4 specularColor = vec4(0.1, 0.1, 0.1, 1);
vec3 eyePosition = vec3(0, 8, 8);

vec4 computeLight(bool doSpecular)
{
	vec4 lightingColor = ambientColor;
	vec3 normal = normalize(pass_normal);
	vec3 normalizedDirection = normalize(lightDirection);
	
	float lambertTerm = dot(normal, normalizedDirection);

	if(lambertTerm > 0.0)
	{
		lightingColor += lightColor * diffuseColor * lambertTerm;

	}

	return lightingColor;
}

void main(void)
{
	// To remove shadow acne
	float bias = 0.005f;
	if(texture(ShadowTexture, pass_shadow_coord.xy).z < pass_shadow_coord.z-0.005f)
		out_frag_color = ambientColor * texture(Texture, pass_texcoord);
	else

	{

	if(TextureEnabled == 1)
		out_frag_color = computeLight(false) * texture(Texture, pass_texcoord);
	else
		out_frag_color = vec4(1,1,1,1);
		}
}